package com.xiaoyu.base.data;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.UpdateBuilder;
import com.xiaoyu.base.db.DBCall;
import com.xiaoyu.base.model.UserKV;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import in.srain.cube.request.JsonData;
import in.srain.cube.util.CLog;

public class UserKVConfigData {

    public static final String KEY_TOKEN_INFO = "token_info";
    public static final String KEY_USER_EXTRA = "user_extra";
    public static final String KEY_EXT_USER_STATUS = "ext_user_status";
    public static final String KEY_IM_TOKEN_PREFIX = "im_token_";
    public static final String KEY_NEW_TIP_DATA = "new_tip_data";

    private static final String TAG = UserKVConfigData.class.getSimpleName();

    private final Map<String, JsonData> map = new HashMap<>();

    public boolean has(String key) {
        return map.containsKey(key);
    }

    public JsonData get(String key) {
        JsonData jsonData = map.get(key);
        return jsonData != null ? jsonData : JsonData.create(null);
    }

    public void put(String key, JsonData jsonData) {
        map.put(key, jsonData);

        new DBCall<UserKV, UserKV, Integer>() {
            @Override
            protected UserKV exec(Dao<UserKV, Integer> dao) throws SQLException {
                final String uid = UserData.getInstance().getUid();

                UserKV current = dao.queryBuilder().where().eq("uid", uid).and().eq("k", key).queryForFirst();
                if (current != null) {
                    UpdateBuilder<UserKV, Integer> updateBuilder = dao.updateBuilder();
                    updateBuilder.where().eq("uid", uid).and().eq("k", key);
                    updateBuilder.updateColumnValue("v", jsonData.toString());
                    updateBuilder.update();
                } else {
                    final UserKV kv = new UserKV();
                    kv.uid = UserData.getInstance().getUid();
                    kv.k = key;
                    kv.v = jsonData.toString();
                    dao.create(kv);
                }
                CLog.d(TAG, "put: %s, %s", key, jsonData);
                return null;
            }
        }.oneWayCall(UserKV.class, true);
    }

    public void reload() {
        loadAllConfigFromDB();
    }

    private void loadAllConfigFromDB() {
        new DBCall<UserKV, UserKV, Integer>() {
            @Override
            protected UserKV exec(Dao<UserKV, Integer> dao) throws SQLException {
                List<UserKV> list = dao.queryBuilder().where().eq("uid", UserData.getInstance().getUid()).query();
                CLog.i(TAG, "loadAllConfigFromDB: %s, %s", UserData.getInstance().getUid(), list);
                for (UserKV item : list) {
                    map.put(item.k, JsonData.create(item.v));
                }
                return null;
            }
        }.oneWayCall(UserKV.class, false);
    }

    public void clear() {
        map.clear();
    }
}
